package com.koala.app;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;


public class DogAdapter extends ArrayAdapter<String> implements Filterable {
	
	private final Context context;
	private List<String> values;
	private List<String> valuesWithoutFilter;
	PopupWindow popupWindowDogOptions;
	private String dogOptionsContent[];
	private String selectedDog;

	public DogAdapter(Context context, List<String> values) {
		super(context, R.layout.dog_list_layout, values);
		this.context = context;
		this.values = values;
		this.valuesWithoutFilter = new ArrayList<String>();
		this.valuesWithoutFilter.addAll(values);
		this.selectedDog = "";

		/* PopUp dog options */
	    
	    // add items on the array dynamically
        List<String> dogOptionsList = new ArrayList<String>();
        dogOptionsList.add(context.getResources().getString(R.string.dog_options_add_service));
        dogOptionsList.add(context.getResources().getString(R.string.dog_options_list_services));
        dogOptionsList.add(context.getResources().getString(R.string.dog_options_edit));
        dogOptionsList.add(context.getResources().getString(R.string.dog_options_delete));
 
        // convert to simple array
        dogOptionsContent = new String[dogOptionsList.size()];
        dogOptionsList.toArray(dogOptionsContent);
 
        // initialize pop up window
        popupWindowDogOptions = popupWindowDogs();
	}

	@Override
	public View getView(int position, final View convertView, ViewGroup parent) {
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View rowView = inflater.inflate(R.layout.dog_list_layout, parent, false);
		final TextView textView = (TextView) rowView.findViewById(R.id.label);
		ImageView imageView = (ImageView) rowView.findViewById(R.id.menu);
		
		if (position < values.size()){
			// Set text <-- DogName(DogBreed) and tag <-- PersonPhone
			String item = values.get(position);
	        String[] itemArr = item.split("::");
	        final String text = itemArr[0];
	        String id = itemArr[1];
			textView.setText(text);
			textView.setTag(id);
			
			
			// Three dots menu onClick listener
			imageView.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					//System.out.println(textView.getText());
					selectedDog = text;
					popupWindowDogOptions.showAsDropDown(v, -5, 0);
			    }
			});
		}
		  
		return rowView;
	}

	/**
	 * Dogs list filter
	 * @param charText
	 */
	public void filter(String charText) {
        charText = charText.toUpperCase();
        values.clear();
        if (charText.length() == 0) {
            values.addAll(valuesWithoutFilter);
        } else {
            for (String s : valuesWithoutFilter) {
                if (s.toUpperCase().startsWith(charText.toString().toUpperCase())) {
                    values.add(s);
                }
            }
        }
        notifyDataSetChanged();
    }
	
	
	/**
	 *  Dog options PopUp Window
	 * @return
	 */
	public PopupWindow popupWindowDogs() {
		 
        // initialize a pop up window type
        PopupWindow popupWindow = new PopupWindow(context);
 
        // the drop down list is a list view
        ListView listViewDogs = new ListView(context);
         
        // set our adapter and pass our pop up window contents
        listViewDogs.setAdapter(dogOptionsAdapter(dogOptionsContent));
         
        // set the item click listener
        listViewDogs.setOnItemClickListener(new OnItemClickListener(){
        	
        	@Override
        	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        		// get the context and main activity to access variables
                Context mContext = view.getContext();
                // add some animation when a list item was clicked
                Animation fadeInAnimation = AnimationUtils.loadAnimation(view.getContext(), android.R.anim.fade_in);
                fadeInAnimation.setDuration(10);
                view.startAnimation(fadeInAnimation);
                 
                // dismiss the pop up
               popupWindowDogOptions.dismiss();
                
                // get the option selected and call the proper function
                String selectedItemText = ((TextView) view).getText().toString();
                String selectedItemTag = ((TextView) view).getTag().toString();
                View xx = (View) view.getParent();
                //Toast.makeText(mContext, "Option selected is: " + selectedItemText + " Tag: " + selectedItemTag, Toast.LENGTH_SHORT).show();
                Toast.makeText(mContext, "Selected dog: " + selectedDog, Toast.LENGTH_SHORT).show();
               /* switch (selectedItemTag) {
	                case "1":
	                	
	                case "2":
	        	        
	            	case "3":
	            	        
	            	case "4":
	        	        
	        	    default:
        	        
        	    }*/
                
        	}
        });
 
        // some other visual settings
        popupWindow.setFocusable(true);
        popupWindow.setWidth(250);
        popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
         
        // set the list view as pop up window content
        popupWindow.setContentView(listViewDogs);
 
        return popupWindow;
    }
	
	
	/**
	 *  Dog options ArrayAdapter
	 * @param dogOptionsArray
	 * @return
	 */
	private ArrayAdapter<String> dogOptionsAdapter(String dogOptionsArray[]) {
		 
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, dogOptionsArray) {
 
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
 
                // setting the ID and text for every items in the list
                String item = getItem(position);
                String[] itemArr = item.split("::");
                String text = itemArr[0];
                String id = itemArr[1];
 
                // visual settings for the list item
                TextView listItem = new TextView(context);
 
                listItem.setText(text);
                listItem.setTag(id);
                listItem.setTextSize(22);
                listItem.setPadding(10, 10, 10, 10);
                listItem.setTextColor(Color.WHITE);
                 
                return listItem;
            }
        };
         
        return adapter;
    }

	/**
	 * @return the popupWindowDogOptions
	 */
	public PopupWindow getPopupWindowDogOptions() {
		return popupWindowDogOptions;
	}
	
	
}
